#include "Generator.h"

Generator::Generator(GamesHistory * gameHist) :  _gameH(gameHist){
}

void Generator::generate(QVector<QString> * _tokens){

    QString token;
    QString tokenType="";
    QString tokenVal="";
    QChar znak;
    QVector<QString> tokenTypes;
    QVector<QString> tokenValues;
    bool tokenTypeRead = true;

    for (int a=0;a<_tokens->length();a++){
        token = _tokens->at(a);
        for (int b=0;b<token.length();b++){
            znak = token[b];
            if (tokenTypeRead){
                if (znak==','){
                    tokenTypeRead=false;
                }else
                    tokenType+=znak;
            }else{
                tokenVal+=znak;
            }
        }
        tokenTypes.append(tokenType);
        tokenValues.append(tokenVal);
        tokenVal="";
        tokenType="";
        tokenTypeRead=true;
    }
    QVector<GameMoves*> gameMovesVec;
    QVector<GameInfo*> gameInfoVec;
    int status = 2; //0-header, 1-moves, 2-break
    int gameReaded=0;
    int indexMove=0;
    bool moveExcepted=false; //0-header, 1-moves
    for (int a=0;a<tokenTypes.length();a++){
        tokenType = tokenTypes.at(a);
        tokenVal = tokenValues.at(a);
        if (tokenType.compare("br")==0){
            if(a>0 && tokenTypes.at(a-1).compare("br")==0){//pusta linia oddzielajaca
                if (status!=2){
                    status=2;
                }
            }
        }else if (tokenType.compare("cL")==0){ //typ naglowka
            if (status==2){
                indexMove=0;
                gameMovesVec.append(new GameMoves());
                gameInfoVec.append( new GameInfo());
            }
            moveExcepted=true;
            status=0;
            a++;
            gameInfoVec.last()->addHeader(tokenVal,tokenValues.at(a));
        }else if (tokenType.compare("nR")==0){ //numer ruchu
            if (status==2){
                status=1;
            }
            indexMove++;
        }else if (tokenType.compare("r1")==0){
            if (a+1<tokenTypes.length() && tokenTypes.at(a+1).compare("k1")==0){
                a++;
                gameMovesVec.last()->addMove(indexMove,"white",tokenVal,tokenValues.at(a));
            }else{
                gameMovesVec.last()->addMove(indexMove,"white",tokenVal);
            }
        }else if (tokenType.compare("r2")==0){
            if (a+1<tokenTypes.length() && tokenTypes.at(a+1).compare("k2")==0){
                a++;
                gameMovesVec.last()->addMove(indexMove,"black",tokenVal,tokenValues.at(a));
            }else{
                gameMovesVec.last()->addMove(indexMove,"black",tokenVal);
            }
        }else if (tokenType.compare("w")==0){
            GameRecord * gameR = new GameRecord(gameInfoVec.last(),gameMovesVec.last());
            _gameH->addGame(gameR);
            gameReaded++;
            indexMove=0;
        }
    }
    Console::print("Wygenerowano poprawnie "+ QString::number(gameReaded)+" gier.");
}


Generator::~Generator() {
}
